package com.xingda.packing.task.generate;

import com.xingda.packing.domain.CacheLocation;
import com.xingda.packing.domain.PackProductionLine;
import com.xingda.packing.domain.ProductBoxLocation;
import com.xingda.packing.enums.BoxLocationNo;
import com.xingda.packing.enums.TaskState;
import com.xingda.packing.enums.WheelType;
import com.xingda.packing.rule.RuleUtilsV2;
import com.xingda.packing.task.ticket.AbstractTaskTicket;
import com.xingda.packing.task.ticket.CachePackingTicket;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 缓存位到装箱位
 *
 * @author LEGION
 * <p>
 * 根据装箱规则带焊点轮数个数，先抓取缓存位带焊点轮子，直到大于等要求的最小值
 */
@Slf4j
@Order(1)
@Component
public class CachePackingGenerate implements TaskGenerateService {
    @Resource
    RuleUtilsV2 ruleUtils;

    @Override
    public AbstractTaskTicket generate(PackProductionLine productionLine, int area) {
        if (!productionLine.getRobot().getWorkStatus(area)) {
            return null;
        }
        if (!productionLine.getRobot().checkCanAssignTaskTicket(area)) {
            return null;
        }
        if (!productionLine.getRobot().canAssignTask(area)) {
            return null;
        }
        if (productionLine.getRobot().haveWheel(area)) {
            return null;
        }
//        校验未完成的任务
        if (!productionLine.getRobot().checkActionStatus(area)) {
            productionLine.getRobot().createAlarmEvent("当前区域存在未完成任务,已停止机器人,请去后台管理页面:[操作管理-动作操作]查看", area);
            log.info("====CachePackingGenerate-当前区域存在未完成任务");
            productionLine.setInitRunning(area, false);
            return null;
        }

        for (ProductBoxLocation productBoxLocation : productionLine.getProductArea().getLocationList(area)) {
            if (productBoxLocation.isEmpty()) {
                continue;
            }
            if (productBoxLocation.getProductBox().isFull()) {
                continue;
            }
            if (productionLine.getRobot().checkUploadStatus(productBoxLocation.getProductBox().getTaskId()) && productBoxLocation.getProductBox().getType() == 1) {
                continue;
            }
            if (!productBoxLocation.isPhotoFinish() || productBoxLocation.isPalletComing()) {
                continue;
            }
//            任务上传状态检测
            if (productionLine.getRobot().checkUploadStatus(productBoxLocation.getProductBox().getTaskId()) && productBoxLocation.getProductBox().getType() == 1) {
                log.info("task:{}任务上传失败", productBoxLocation.getProductBox().getTaskId());
                continue;
            }
            List<CacheLocation> cacheLocationList = new ArrayList<>();
            for (CacheLocation cacheLocation : productionLine.getCacheArea().getLocationList(area)) {
                if (cacheLocation.isEmpty()) {
                    continue;
                }
                if (ruleUtils.canPut(productBoxLocation.getProductBox(), cacheLocation.getWheel())) {
                    cacheLocationList.add(cacheLocation);
                }
            }
            if (CollectionUtils.isEmpty(cacheLocationList)) {
                continue;
            }
            CacheLocation optionLocation = ruleUtils.getOptimalWheel(productBoxLocation.getProductBox(), cacheLocationList);
            if (Objects.nonNull(optionLocation)) {
                CachePackingTicket ticket = new CachePackingTicket();
                ticket.setState(TaskState.NEW);
                ticket.setWheel(optionLocation.getWheel());
                ticket.setCacheLocationNo(optionLocation.getNo());
                ticket.setProductBoxLocationNo(BoxLocationNo.getById(productBoxLocation.getLocationNo()).getNo());
                ticket.setProductType(WheelType.getByTypeName(optionLocation.getWheel().getVFree2()).getType());
                ticket.setTag(productBoxLocation.getLocationNo());
                ticket.setArea(area);
                productionLine.getRobot().createCachePackingEvent(ticket, area);
                return ticket;
            }
        }
        productionLine.getRobot().setWorkStatus(false, area);
        return null;
    }
}
